snippet main "main function" 
// 按引用传递对象的方式称作借用 
fn main() {
	${1:println!("Hello, world!");}
}
endsnippet

snippet function "function declare"
fn ${1:name}(${2:arg1: i32}) -> ${3:i32} {
	$0
}
endsnippet

snippet type_of "return a string representation of the type"
// println!("The type of x is: {}", type_of(&x)); 
fn type_of<T>(_: &T) -> String {
    format!("{}", std::any::type_name::<T>())
}
endsnippet

snippet var "declare a variable"
let ${1:mut} ${2:name}:${3:type} = $4
endsnippet

snippet array1 "Declares an array and initializes its elements" 
let ${1:name}: [${2:type}; ${3:len}] = [${4:1, 2, 3, 4, 5}];
endsnippet

snippet array2 "Initialize the array with the default values" 
let ${1:name}: [${2:type}; ${3:len}] = [${4:55}; $3];
endsnippet

snippet array3 "Variable length dynamic array" 
let ${1:name}: Vec<${2:i32>} = vec![${3:1, 2, 3, 4, 5}];
endsnippet

snippet vector "variable-length vector" 
let ${1:vec} = vec![$3 ; ${2:len}];
endsnippet

snippet tuple "group together some types into one compound type" 
let ${1:name}:(${2:typ1, typ2, ...}) = ($3);
endsnippet

snippet struct_declare "name-field" 
// struct内变量默认私有, pub修饰语使变量公有
pub struct ${1:name} {
	${2:id: i32,
	balance: i64, }
}
endsnippet

snippet struct_impl "Description"
impl ${1:name} {
	pub fn new(${2:id: i32}) -> Self {
		$1 {
			Self {
				id, 
				balance: 0i64,
			}
		}
	}

	// &mult self, 修改变量, 否则直接使用&self
	pub fn deposit(&mut self, amount: i64) {
		self.balance += amount;
	}
}

endsnippet

snippet enum "enum"
enum ${1:name} {
	${2:Variant1,}
	${3:Variant2,}
}

endsnippet

snippet trait "A common interface for a group of types."
trait ${1:name} {
	fn ${2:function}(&self) -> i32;
	fn $2(self: &Self) -> i32;
}

impl ${3:Struct} for ${1:name} {
	fn $2(&self) -> i32 {
		$0
	}
}
endsnippet

############################### flow ######################################
snippet if "if --- else" b
if ${1:age > 18} {
    ${VISUAL:default}
}
endsnippet

snippet if_let "if --- let" b
let ${1:num} = if ${2:condition} { $3 } else { $4 };
endsnippet

snippet for "for ... in ..."
for ${1:i} in ${2:1..=10} {
    $3
}
endsnippet

snippet while "Loop while a condition is upheld"
while ${1:cont} {
    ${VISUAL:default}
}
endsnippet

snippet loop "Loop indefinitely"
${1:'label} loop {
    $1 
    break;
}
endsnippet

snippet match ""
match ${1:expression} {
    {${2:0}} => {

    },
    {${3:1..2}} => {

    },
	- => (),
}
endsnippet

snippet function "function template" b
fn ${1:name}(${2:mut var1:i32}, ${3:var2:&mut i32}) -> ${4:ret_type} {
    //隐式返回最后的表达式(没有分号)
}
endsnippet

snippet closure "lambda expression"
let ${1:closure_name} = |${2: parameter}| -> ${3:ret_value} { $4 }
endsnippet


############################### file ######################################
snippet file_open "open file"
use std::fs::File;
use std::io::Read;

fn main() {
    // 打开文件
    let mut file = match File::open("path/to/file.txt") {
        Ok(file) => file,
        Err(error) => panic!("无法打开文件: {:?}", error),
    };

    // 创建一个缓冲区来存储文件内容
    let mut buffer = String::new();

    // 读取文件内容到缓冲区
    match file.read_to_string(&mut buffer) {
        Ok(_) => {
            // 成功读取文件内容
            println!("文件内容:\n{}", buffer);
        }
        Err(error) => {
            // 读取文件内容时发生错误
            panic!("无法读取文件: {:?}", error);
        }
    }
}
endsnippet

snippet file_write "write data to file"
use std::fs::File;
use std::io::Write;

fn main() {
    // 创建一个文件并打开以进行写入
    let mut file = match File::create("path/to/file.txt") {
        Ok(file) => file,
        Err(error) => panic!("无法创建文件: {:?}", error),
    };

    // 写入内容到文件
    match file.write_all(b"Hello, World!") {
        Ok(_) => {
            // 写入成功
            println!("写入文件成功！");
        }
        Err(error) => {
            // 写入文件时发生错误
            panic!("无法写入文件: {:?}", error);
        }
    }
}
endsnippet

############################### thread ######################################
snippet thread_create "create thread"

endsnippet


snippet tests
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_${1:function_name}() {
		$2
    }
}
endsnippet
